package com.appspot.etao.service;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.appspot.etao.Constant;
import com.appspot.etao.jdo.PMF;
import com.appspot.etao.model.ItemCatModel;
import com.appspot.etao.util.ConvertUtil;
import com.taobao.top.domain.ItemCat;

public class ItemCatService extends BaseService {

	public void getTopData() {
		//将Top ItemCat数据保存到本地数据库
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		TopService topService = ServiceFactory.getTopService();
		
		System.out.println("CronService: Start getItemCat");
		List<ItemCat> itemCatList = topService.getItemCatList();
		List<ItemCatModel> itemCatModelList = ConvertUtil.convertItemCatList(itemCatList);
		pm.makePersistentAll(itemCatModelList);
		System.out.println("CronService: End getItemCat");
		
		pm.close();
	}
	
	public List<ItemCatModel> getAllItemCatList() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		String itemCatQuery = "select from " + ItemCatModel.class.getName();
		List<ItemCatModel> itemCatList = (List<ItemCatModel>)pm.newQuery(itemCatQuery).execute();
		
		pm.close();
		
		return itemCatList;
	}
	
	public List<ItemCatModel> getItemCatList(int block) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query itemCatCatchQuery = pm.newQuery(ItemCatModel.class);
		itemCatCatchQuery.setFilter("isCatched = false");
		itemCatCatchQuery.setRange(0, block);
		
		List<ItemCatModel> itemCatModelList = (List<ItemCatModel>)itemCatCatchQuery.execute();
		
		pm.close();
		
		return itemCatModelList;
	}
	
	public boolean updateItemCat(List<ItemCatModel> itemCatModelList) {
		boolean isSuccess = false;
		
		
		
		return isSuccess;
	}
}
